import { chatRequest } from '@/utils/request'



/**
 * 聊天接口，调用知识库
 * @returns 
 */
export const chatQuestion = ({question,token}) =>
    chatRequest.post('/ai/chat',{ question,token },{
        headers: {
            'Content-Type': 'application/json'
        }
    });
/**
 * 流式聊天
 * @param {} data 
 * @returns 
 */
export const streamChatQuestion = ({question,userId},onData,onError,onComplete) =>
 chatRequest.post('/ai/sse',{ question,userId },{
    headers:{
        'Content-Type': 'application/json',
        'Accept': 'text/event-stream'
    },
    responseType:'stream',
    onDownloadProgress: (progressEvent) => {
    // 处理流式数据
    const response = progressEvent.event.target.response;
    const lines = response.split('\n\n').filter(line => line.trim());
    
    lines.forEach(line => {
        if (line.startsWith('data: ')) {
            const data = line.replace('data: ', '');
            onData?.(data);
        } else if (line.startsWith('event: complete')) {
            onComplete?.();
        }
    });
}
}).catch(error => {
onError?.(error);
throw error;
});


/**
 * 从Redis中获取历史聊天记录
 * @param {*} param0 
 * @returns 
 */
export const chatHistory = (token) => 
    chatRequest.get('/ai/gethistoryList', {
      params: { token } // ✅ 正确写法
    });
export const getChun = ({question}) =>
    chatRequest.get('/ai/chun',{params:{question}});

